package com.gem.growingtree.controller;

import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gem.growingtree.model.PageBean;
import com.gem.growingtree.model.ShowParents;
import com.gem.growingtree.service.ParentAccountService;


@Controller
public class ParentAccountController {
	@RequestMapping("/parentAccount")
	public ModelAndView ShowParentAccount(HttpServletRequest request, HttpServletResponse response){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		ParentAccountService pas = new ParentAccountService();
		try {
			PageBean pb = pas.findByPage(pageCode, pageSize);
			request.setAttribute("pageBean", pb);
			return new ModelAndView("/parents");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 查找某个家长信息
	@RequestMapping("/showCertainParent")
	public ModelAndView ShowParentAccount2(HttpServletRequest request, HttpServletResponse response,String showP){
		int pageCode = 0;
		String pc = request.getParameter("pc"); 
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		ParentAccountService pas = new ParentAccountService();
		try {
			PageBean pb = pas.findByPage2(pageCode, pageSize,showP);
			request.setAttribute("pageBean", pb);
			return new ModelAndView("/parents");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	

	//显示要修改的家长信息
	@RequestMapping("/showDetailParent")
	public ModelAndView showCertainParent3(HttpServletRequest request, HttpServletResponse response,String userId) throws UnsupportedEncodingException{
		userId = request.getParameter("userId");
		request.setCharacterEncoding("UTF-8");
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		ParentAccountService pas = new ParentAccountService();
		try {
			PageBean pb = pas.findByPage2(pageCode, pageSize,userId);
			request.setAttribute("pageBean", pb);
			return new ModelAndView("/parent");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 修改某个家长信息
	@RequestMapping("/modifyParent")
	public String modifyParent(HttpServletRequest request,
			HttpServletResponse response, ShowParents showParents) {
		String userId = request.getParameter("userId");
		if (userId != null) {
			try {
				ParentAccountService psParentService = new ParentAccountService();
				psParentService.updateParents(Integer.parseInt(userId),showParents);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "/modifyParent";
	}
	//删除某个家长的信息
	@RequestMapping("/deleteParents")
	public ModelAndView deleteParentInfo( HttpServletRequest request,
			HttpServletResponse response) throws Exception {	

		String  parentId = request.getParameter("parentId");
		ParentAccountService ps = new ParentAccountService();
		ps.deleteParentInfo(parentId);
		int current = 0;
		PageBean  pageBean	 = ps.findByPage(current, 4);
		request.setAttribute("pageBean", pageBean);
		return new ModelAndView("/parents", "pageBean", pageBean);
	}
}
